package com.mt.mtyx.activity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.mtyx.activity.service.ActivityInfoService;
import com.mt.mtyx.common.exception.BusinessException;
import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.dto.activity.ActivityRuleDto;
import com.mt.mtyx.entity.activity.ActivityInfo;
import com.mt.mtyx.entity.product.SkuInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * 营销活动控制层
 *
 * @author WMT
 * @Date 2023/7/24 15:53
 */
@Api(tags = "营销活动")
@RequestMapping("/admin/activity/activityInfo")
@RestController
public class ActivityInfoController {

    @Autowired
    private ActivityInfoService activityInfoService;

    @ApiOperation("分页查询活动列表")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<ActivityInfo>> selectAll(
            @PathVariable Long page,
            @PathVariable Long limit
    ) {
        return Result.success(activityInfoService.selectAll(new Page<ActivityInfo>(page, limit)));
    }

    @ApiOperation("获取活动详情")
    @GetMapping("/get/{id}")
    public Result<ActivityInfo> get(@PathVariable Long id) {
        ActivityInfo activityInfo = activityInfoService.getById(id);
        if (activityInfo != null) {
            activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        }
        return Result.success(activityInfo);
    }

    @ApiOperation("添加活动")
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody ActivityInfo activityInfo) {
        if (StringUtils.isEmpty(activityInfo.getActivityName())) {
            throw new BusinessException("活动名称不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (activityInfoService.existsByName(activityInfo.getActivityName())) {
            throw new BusinessException("活动名称已存在！", HttpStatus.BAD_REQUEST.value());
        }
        if (StringUtils.isEmpty(activityInfo.getActivityType())) {
            throw new BusinessException("活动类型不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(activityInfoService.insert(activityInfo));
    }

    @ApiOperation("删除活动")
    @DeleteMapping("/remove/{id}")
    public Result<Integer> save(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(activityInfoService.delete(id));
    }

    @ApiOperation("批量删除活动")
    @DeleteMapping("/batchRemove")
    public Result<Integer> save(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(activityInfoService.batchDelete(ids));
    }

    @ApiOperation("修改活动")
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody ActivityInfo activityInfo) {
        if (StringUtils.isEmpty(activityInfo.getId())) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (StringUtils.isEmpty(activityInfo.getActivityName())) {
            throw new BusinessException("活动名称不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        ActivityInfo activityInfoDb = activityInfoService.getById(activityInfo.getId());
        if (!activityInfoDb.getActivityName().equals(activityInfo.getActivityName())) {
            if (activityInfoService.existsByName(activityInfo.getActivityName())) {
                throw new BusinessException("活动名称已存在！", HttpStatus.BAD_REQUEST.value());
            }
        }
        if (StringUtils.isEmpty(activityInfo.getActivityType())) {
            throw new BusinessException("活动类型不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(activityInfoService.update(activityInfo));
    }

    /* ================================= 营销活动规则相关接口 ================================= */

    @ApiOperation("根据活动id获取活动规则列表")
    @GetMapping("/findActivityRuleList/{activityId}")
    public Result<Map<String, Object>> findActivityRuleList(@PathVariable Long activityId) {
        if (StringUtils.isEmpty(activityId)) {
            throw new BusinessException("活动id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(activityInfoService.getActivityRuleListById(activityId));
    }

    @ApiOperation("添加活动规则")
    @PostMapping("/saveActivityRule")
    public Result<Integer> saveActivityRule(@RequestBody ActivityRuleDto dto) {
        if (StringUtils.isEmpty(dto.getActivityId())) {
            throw new BusinessException("活动id不能为空！",HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(activityInfoService.saveActivityRule(dto));
    }

    @ApiOperation("根据关键词获取商品详情")
    @GetMapping("/findSkuInfoByKeyword/{keyword}")
    public Result<List<SkuInfo>> findSkuInfoByKeyword(@PathVariable String keyword) {
        if (StringUtils.isEmpty(keyword)) {
            throw new BusinessException("请输入正确的关键词！",HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(activityInfoService.getSkuInfoByKeyword(keyword));
    }
}
